﻿Imports TMySQL
Public Class frmSQL
    Implements ITabForm

    Private MyQuery As New TMyQuery
    Private Const DEFAULT_LIMIT = 30
    Private Const DEFAULT_OFFSET = 0

    Public Sub New()
        ' この呼び出しは、Windows フォーム デザイナで必要です。
        InitializeComponent()
        ' InitializeComponent() 呼び出しの後で初期化を追加します。
        MyQuery.Session = frmMain.Connection
        InitFrm()
    End Sub

    Private Sub InitFrm()
        frmMain.GeneralSettings.ApplyAll(Me)
        frmMain.EditorSettings.ApplyAll(Me)
        frmMain.GridSettings.ApplyAll(Me)
    End Sub

    Private Sub cmdSQL_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSQL.Click
        If txtSQL.Text.Trim.Length <= 0 Then
            Call MsgBox("SQLを入力してください", MsgBoxStyle.Exclamation)
            Return
        End If
        Cursor.Current = Cursors.WaitCursor
        Try
            ExecuteSQL(txtSQL.Text)
        Catch ex As Exception
            Call MsgBox(ex.Message, MsgBoxStyle.Exclamation)
        Finally
            Cursor.Current = Cursors.Default
        End Try
    End Sub

    Private Function GetSQLType(ByVal ASQL As String) As TMyQuery.TSQLType
        Dim s As String = ASQL.Trim.ToUpper
        If s.Substring(0, 6).Equals("SELECT") OrElse s.Substring(0, 4).Equals("SHOW") OrElse s.Substring(0, 7).Equals("EXPLAIN") Then
            Return TMyQuery.TSQLType.stSelect
        ElseIf s.Substring(0, 4).Equals("INSERT") Then
            Return TMyQuery.TSQLType.stInsert
        ElseIf s.Substring(0, 6).Equals("UPDATE") Then
            Return TMyQuery.TSQLType.stUpdate
        ElseIf s.Substring(0, 6).Equals("DELETE") Then
            Return TMyQuery.TSQLType.stDelete
        ElseIf s.Substring(0, 7).Equals("REPLACE") Then
            Return TMyQuery.TSQLType.stReplace
        ElseIf s.Substring(0, 6).Equals("CREATE") Then
            Return TMyQuery.TSQLType.stCreate
        ElseIf s.Substring(0, 5).Equals("ALTER") Then
            Return TMyQuery.TSQLType.stAlter
        ElseIf s.Substring(0, 4).Equals("DROP") Then
            Return TMyQuery.TSQLType.stDrop
        ElseIf s.Substring(0, 8).Equals("TRUNCATE") Then
            Return TMyQuery.TSQLType.stTruncate
        Else
            Return TMyQuery.TSQLType.stEtc
        End If

    End Function

    Private Sub ExecuteSQL(ByVal ASQL As String)
        Dim myStatus As New TMyStatus(MyQuery.Session)
        Try
            MyQuery.Close()
            If cmdStatus.Checked Then
                myStatus.FlushStatus()
            End If
            If cmdResult.Checked Then
                MyQuery.Clear()
                MyQuery.SQL.Append(ASQL)
                MyQuery.Execute()
                If GetSQLType(ASQL) = TMyQuery.TSQLType.stSelect Then
                    Grid.DataSource = MyQuery.DataSet.Tables(0)
                Else
                    Call MsgBox(MyQuery.AffectedRows.ToString & "件のデータを処理しました", MsgBoxStyle.Information)
                End If
            End If

            If cmdStatus.Checked Then
                ShowStatus(myStatus)
            End If
            If cmdPlan.Checked Then
                ShowPlan(ASQL)
            End If
        Finally
            myStatus.Dispose()
        End Try
    End Sub

    Private Sub ShowStatus(ByVal AStatus As TMyStatus)
        Dim sp As Splitter
        AddStatusSheet(New frmStatus(AStatus.CreatedStatus, "Create Status"))
        sp = New Splitter
        sp.Parent = tbsStatus

        AddStatusSheet(New frmStatus(AStatus.SortStatus, "Sort Status"))
        sp = New Splitter
        sp.Parent = tbsStatus

        AddStatusSheet(New frmStatus(AStatus.HandlerStatus, "Handler Status"))
        sp = New Splitter
        sp.Parent = tbsStatus

        AddStatusSheet(New frmStatus(AStatus.SelectStatus, "Select Status"))
    End Sub

    Private Sub AddStatusSheet(ByVal AForm As frmStatus)
        AForm.TopLevel = False
        AForm.Parent = tbsStatus
        AForm.Dock = DockStyle.Left
        AForm.Show()
    End Sub

    Private Sub ShowPlan(ByVal ASQL As String)
        Dim frmRet As New frmExplain
        frmRet.SQL = ASQL
        frmRet.TopLevel = False
        frmRet.Parent = tbsPlan
        frmRet.Dock = DockStyle.Fill
        frmRet.Show()
    End Sub

    Private Sub cmdSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSave.Click
        If SaveDlg.ShowDialog <> Windows.Forms.DialogResult.OK Then
            Return
        End If
        Dim sw As New System.IO.StreamWriter(SaveDlg.FileName)
        Cursor.Current = Cursors.WaitCursor
        Try
            sw.Write(txtSQL.Text)
            sw.Flush()
            Call MsgBox("保存しました", MsgBoxStyle.Information)
        Finally
            sw.Close()
            Cursor.Current = Cursors.Default
        End Try
    End Sub

    Private Sub cmdSaveData_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSaveData.Click
        If SaveDataDlg.ShowDialog <> Windows.Forms.DialogResult.OK Then
            Return
        End If
        Dim sw As New System.IO.StreamWriter(SaveDataDlg.FileName)

        Cursor.Current = Cursors.WaitCursor
        Try
            If SaveDataDlg.FilterIndex = 1 Then
                sw.Write(MyQuery.DataSetText(TMyQuery.TColSeparator.csComma))
            Else
                sw.Write(MyQuery.DataSetText(TMyQuery.TColSeparator.csTab))
            End If
            Call MsgBox("保存しました", MsgBoxStyle.Information)
        Finally
            Cursor.Current = Cursors.Default
        End Try
    End Sub

    Private Sub cmdOpen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdOpen.Click
        If OpenDlg.ShowDialog <> Windows.Forms.DialogResult.OK Then
            Return
        End If
        Dim sr As New System.IO.StreamReader(OpenDlg.FileName)
        Cursor.Current = Cursors.WaitCursor
        Try
            txtSQL.Text = sr.ReadToEnd
        Finally
            sr.Close()
            Cursor.Current = Cursors.Default
        End Try
    End Sub

    Private Sub cmdCopy_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton5.Click
        Cursor.Current = Cursors.WaitCursor
        Try
            Clipboard.SetText(MyQuery.DataSetText(TMyQuery.TColSeparator.csTab))
            Call MsgBox("コピーしました", MsgBoxStyle.Information)
        Finally
            Cursor.Current = Cursors.Default
        End Try
    End Sub

    Private Sub txtLimit_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs)
        If e.KeyChar < "0"c OrElse e.KeyChar > "9"c Then
            e.Handled = True
        End If
    End Sub

    Private Sub txtOffSet_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs)
        If e.KeyChar < "0"c OrElse e.KeyChar > "9"c Then
            e.Handled = True
        End If
    End Sub
End Class